<?php
include('connect.php');
echo $shoppingcart_id=$_COOKIE["shoppingcart_id"];
print_r($_cookie);
if(!isset($_COOKIE["shoppingcart_id"]))
     {
     function generateid()
	{
	$nrC = 0;
	$chk_code = "";
	$char = "";
	while($nrC <= 9)
		{ 
		$c = rand(48, 90); //generates a casual number between 48 and 90
		if( ($c >= 48) && ($c <= 57) )
			{
			//rand generated a digit
			$char = chr($c);
			$chk_code .= $char;
			$char = "";
			$nrC = $nrC + 1;
			}
		if( ($c >=65) && ($c <= 90) )
			{
			//rand generated a letter
			$char = chr($c);
			$chk_code .= $char;
			$char = "";
			$nrC = $nrC + 1;
			}
		}
	return $chk_code;
	}

//generate a random 10 characters code
$shoppingcart_id = generateid();

//get the current date
$today = date("Y-m-d");

//get the users's ip
$user_ip = $_SERVER['REMOTE_ADDR'];

//now, you can register the shopping cart into the DB
mysql_query("INSERT INTO carts (ID, DATE, IP) VALUES ('$shoppingcart_id' , '$today', '$user_ip')");

//after registering the new cart, set the cookie to the user's browser

//set the lifetime of the cookie
$expire = time()+60*60*24*30; // (1 month lifetime)

//set the domain for which the cookie is valid
$dom = $_SERVER['SERVER_NAME'];
if($dom[0] == "w")
	{
	//truncate the first three characters
	$domain = substr($dom, 3);
	}
else
	{
	//add point
	$domain = ".".$dom;
	}

//finally, send the cookie to the user agent
$expire=time()+60*60*24*30;
//setcookie("shoppingcart_id", "Alex Porter", $expire);
setcookie("shoppingcart_id", $shoppingcart_id, $expire);
     
     }
else
     {
     $shoppingcart_id = $_COOKIE["shoppingcart_id"];

//interrogate the database to see how many items are contained
/*$items = mysql_query("SELECT COUNT(*) AS NR FROM rel WHERE ID_CART = '$shoppingcart_id'");

//extract the array from the sql resulting table
$item_nr = mysql_fetch_array($items);

//display information
echo("Your cart contains ".$item_nr["NR"]." items");*/
$items = mysql_query("SELECT * FROM rel WHERE ID_CART = '$shoppingcart_id'");

//extract the array from the sql resulting table
//$item_nr = mysql_fetch_array($items);
$NR=mysql_num_rows($items);

//display information
echo("Your cart contains ".$NR." items");
?>
<a href="mycart.php?shoppingcart_id=<?php echo $shoppingcart_id;?>">My Cart</a>
<?php
     
     }

?>
<?php
//select all the items into the database and show them to screen
$items = mysql_query("SELECT ID, NAME, PRICE, IMAGE_URL, DESCRIPTION FROM items");

//extracting every single item
while($item = mysql_fetch_array($items))
	{
	?>
    <img src="<?php echo($item["IMAGE_URL"]); ?>" /><br />
    Item name: <?php echo($item["NAME"]); ?><br />
    Item description: <?php echo($item["DESCRIPTION"]); ?><br />
    <form action="mypage.php" method="post">
	<input type="hidden" name="shoppingcart_id" value="<?php echo $shoppingcart_id;?>" /> 
	<input type="hidden" name="action" value="add" />
        <input type="hidden" name="item" value="<?php echo($item["ID"]); ?>" />
        <input type="text" name="quantity" /><br />
        <input type="submit" value="Add to cart" />
    </form>
    <?php
	}
?>